package com.qianya.model.common;

import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.io.Serializable;

/**
 * @Package: com.qianya.model.common
 * @ClassName: CommonResult
 * @Author: zzq
 * @Description: 通用返回类
 * @Date: 2020/6/9 14:22
 * @Version: 1.0
 */
@Data
public class CommonResult<T> implements Serializable {

    private Integer code;
    private String message;
    private T bean;

    /**
     * 正确数据返回
     *
     * @return CommonResult
     */
    public static <T> CommonResult<T> success() {
        CommonResult<T> result = new CommonResult<>();
        result.setBean(null);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    /**
     * 正确数据返回
     *
     * @param bean 数据对象
     * @return CommonResult
     */
    public static <T> CommonResult<T> success(T bean) {
        CommonResult<T> result = new CommonResult<>();
        result.setBean(bean);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    /**
     * 正确数据返回
     *
     * @param bean    数据对象
     * @param message 自定义成功信息
     * @return CommonResult
     */
    public static <T> CommonResult<T> success(T bean, String message) {
        CommonResult<T> result = new CommonResult<>();
        result.setBean(bean);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 错误数据返回
     *
     * @param resultCode 错误类型
     * @return CommonResult
     */
    public static <T> CommonResult<T> error(ResultCode resultCode) {
        CommonResult<T> result = new CommonResult<>();
        result.setBean(null);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

    /**
     * 错误数据返回
     *
     * @param resultCode 错误类型
     * @param data       错误数据
     * @return CommonResult
     */
    public static <T> CommonResult<T> error(ResultCode resultCode, T data) {
        CommonResult<T> result = new CommonResult<>();
        result.setBean(data);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

    /**
     * 错误数据返回
     *
     * @param resultCode 错误类型
     * @param message    自定义错误信息
     * @return CommonResult
     */
    public static <T> CommonResult<T> error(ResultCode resultCode, String message) {
        CommonResult<T> result = new CommonResult<>();
        result.setBean(null);
        result.setCode(resultCode.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 错误数据返回
     *
     * @param resultCode 错误类型
     * @param data       自定义错误数据
     * @param message    自定义错误信息
     * @return CommonResult
     */
    public static <T> CommonResult<T> error(ResultCode resultCode, T data, String message) {
        CommonResult<T> result = new CommonResult<>();
        result.setBean(data);
        result.setCode(resultCode.getCode());
        result.setMessage(message);
        return result;
    }


}
